<?php


namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use App\Models\BusinessUser;
use App\Models\SmsLog;
use Illuminate\Http\Request;
use Overtrue\EasySms\EasySms;

class AdminUserController extends Controller
{
    function request_by_curl($remote_server, $post_string)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $remote_server);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=utf-8'));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
        // curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
        // curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }

    public function dingding(Request $request)
    {
        $message = $request->input('msg');
        $token = 'a332104089db688c02a9420cfa22944109efaff2b0000ece6907ac4f8b206136';
        $se = 'SECc8a262406c941b0062b8eaa37e30787142eb3f436c2243fdec7afbeb42c6b9a9';
        $sign = $this->hashEode($se);
        $webhook = "https://oapi.dingtalk.com/robot/send?access_token=" . $token . "&timestamp=" . $sign['timestamp'] . '&sign=' . $sign['sign'];
//        $message = "我就是我, 是不一样的烟火";
        $data = [
            'msgtype' => 'text',
            'text' =>
                [
                    'content' => $message
                ],
            "at" => [
                "atMobiles" => [

                ],
                "isAtAll" => false
            ]
        ];
        $data_string = json_encode($data);
        $result = $this->request_by_curl($webhook, $data_string);
        echo $result;
    }

    public function hashEode($data)
    {
        $time = time() * 1000;
        $sign = urlencode(base64_encode(hash_hmac("sha256", $time . "\n" . $data, $data, true)));
        return ['timestamp' => $time, 'sign' => $sign];
    }

}